# Code for Table O1: Share of compliers across severity terciles

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

library('AER')
library('ivpack')
library('dplyr')
library('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------

#load the dataset
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))


## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift)"
case.controls <- "as.factor(any_drug_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"


data0812$comp_w <- NA

for(i in 1:3) {
    form.3.cc <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
    out <- lm(form.3.cc, data = data0812[totOGS2 == i, ])  
    quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == i], c(0.01, 0.99))
    frac_complier <- out$coefficients["judgeiv"] * quants.iv[2] - out$coefficients["judgeiv"] * quants.iv[1]
    mean_frac <- sum(data0812$totOGS2 == i)/nrow(data0812)
    data0812$comp_w[data0812$totOGS2 == i] <- frac_complier/mean_frac
}

## -------------------------------
## Fraction of compliers:
## we define compliers as defendants whose pre-trial detention
## decision would have been different had their case been assigned to the most strict instead of the
## most lenient judge
## -------------------------------
## Row 1: Compliers
## -------------------------------
out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 1, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 1], c(0.01, 0.99))
r1c1 <- round(coef(out.1)[2] * quants.iv[2] - coef(out.1)[2] * quants.iv[1],3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 2, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 2], c(0.01, 0.99))
r1c2 <- round(coef(out.1)[2] * quants.iv[2] - coef(out.1)[2] * quants.iv[1], 3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 3, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 3], c(0.01, 0.99))
r1c3 <- round(coef(out.1)[2] * quants.iv[2] - coef(out.1)[2] * quants.iv[1], 3)

r1 <- cbind(r1c1, r1c2, r1c3)

## -------------------------------
## Row 2: Always Takers
## -------------------------------

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 1, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 1], c(0.01, 0.99))
r2c1 <- round(coef(out.1)[1] + coef(out.1)[2] * quants.iv[1],3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 2, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 2], c(0.01, 0.99))
r2c2 <- round(coef(out.1)[1] + coef(out.1)[2] * quants.iv[1],3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 3, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 3], c(0.01, 0.99))
r2c3 <- round(coef(out.1)[1] + coef(out.1)[2] * quants.iv[1],3)
r2 <- cbind(r2c1, r2c2, r2c3)

## -------------------------------
## Row 3: Never Takers
## -------------------------------

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 1, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 1], c(0.01, 0.99))
r3c1 <- round(1 - coef(out.1)[1] - coef(out.1)[2] * quants.iv[2],3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 2, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 2], c(0.01, 0.99))
r3c2 <- round(1 - coef(out.1)[1] - coef(out.1)[2] * quants.iv[2],3)

out.1 <- lm(form.3.cc, data = data0812[totOGS2 == 3, ])  
quants.iv <- quantile(data0812$judgeiv[data0812$totOGS2 == 3], c(0.01, 0.99))
r3c3 <- round(1 - coef(out.1)[1] - coef(out.1)[2] * quants.iv[2],3)
r3 <- cbind(r3c1, r3c2, r3c3)

## -------------------------------
# Table O1
## -------------------------------
cont <- as.data.frame(rbind(r1, r2, r3))
colnames(cont) <- c("Low", "Medium", "High")
rownames(cont) <- c("Compliers", "Always Takers", "Never Takers")
cat("\nPrinting Table O1: Share of Compliers Across Severity Levels...\n")
print(cont)


